﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Text;
using Infragistics.Win.UltraWinGrid;
using System.Windows.Forms;
using System.Data;
using System.Data.SqlClient;

namespace AutoParkingManagement
{
    public partial class EditCard : Form
    {
        #region Private members
        private UltraGridRow _selectedRow;

        private bool ValidateData()
        {
            if (cboCardType.Value == null)
            {
                MessageBox.Show("Vui lòng chọn loại thẻ xe.");
                cboCardType.Select();
                return false;
            }

            if (dtpOrderDateFrom.Value != null && dtpOrderDateTo.Value != null)
            {
                if ((DateTime)dtpOrderDateFrom.Value > (DateTime)dtpOrderDateTo.Value)
                {
                    MessageBox.Show("Giá trị của [từ ngày] phải nhỏ hơn [đến ngày].");
                    dtpOrderDateFrom.Select();
                    return false;
                }
            }
            return true;
        }
        #endregion

        #region Properties
        #endregion

        #region Public members
        public delegate void UpdateCardHandler(UltraGridRow row);
        public event UpdateCardHandler UpdateCard;
        #endregion

        #region Events
        private void EditCard_Load(object sender, EventArgs e)
        {
            txtCardID.Text = _selectedRow.Cells["CardID"].Value.ToString();
            cboCardType.DataSource = Common.GetCardTypes(false);
            cboCardType.DisplayMember = "Name";
            cboCardType.ValueMember = "CardTypeID";
            if (_selectedRow.Cells["CardTypeID"].Value != null)
            {
                cboCardType.Value = _selectedRow.Cells["CardTypeID"].Value;
            }

            if (_selectedRow.Cells["VIP"].Value != null)
            {
                chkVIP.Checked = (bool)_selectedRow.Cells["VIP"].Value;
            }

            if (_selectedRow.Cells["FromDate"].Value != null)
            {
                dtpOrderDateFrom.Value = _selectedRow.Cells["FromDate"].Value;
            }
            if (_selectedRow.Cells["ToDate"].Value != null)
            {
                dtpOrderDateTo.Value = _selectedRow.Cells["ToDate"].Value;
            }

            if (_selectedRow.Cells["Active"].Value != null)
            {
                chkActive.Checked = (bool)_selectedRow.Cells["Active"].Value;
            }
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            try
            {
                if (!ValidateData()) return;
                StringBuilder sb = new StringBuilder();
                sb.Append("UPDATE Card ");
                sb.Append("SET CardTypeID = @CardTypeID, VIP = @VIP, ");
                sb.Append("FromDate = @FromDate, ToDate = @ToDate, Active=@Active, LastUser=@LastUser, LastUpdate = GetDate() ");
                sb.Append("WHERE CardID = @CardID");
                SqlParameter[] parms = new SqlParameter[7];
                parms[0] = new SqlParameter();
                parms[0].ParameterName = "@CardTypeID";
                parms[0].SqlDbType = SqlDbType.UniqueIdentifier;
                parms[0].Value = cboCardType.Value;

                parms[1] = new SqlParameter();
                parms[1].ParameterName = "@VIP";
                parms[1].SqlDbType = SqlDbType.Bit;
                parms[1].Value = chkVIP.Checked;

                parms[2] = new SqlParameter();
                parms[2].ParameterName = "@FromDate";
                parms[2].SqlDbType = SqlDbType.DateTime;
                if (dtpOrderDateFrom.Value == null)
                {
                    parms[2].Value = DBNull.Value;
                }
                else
                {
                    parms[2].Value = dtpOrderDateFrom.Value;
                }

                parms[3] = new SqlParameter();
                parms[3].ParameterName = "@ToDate";
                parms[3].SqlDbType = SqlDbType.DateTime;
                if (dtpOrderDateTo.Value == null)
                {
                    parms[3].Value = DBNull.Value;
                }
                else
                {
                    parms[3].Value = dtpOrderDateTo.Value;
                }


                parms[4] = new SqlParameter();
                parms[4].ParameterName = "@CardID";
                parms[4].SqlDbType = SqlDbType.NVarChar;
                parms[4].Size = 50;
                parms[4].Value = txtCardID.Text;
                parms[5] = new SqlParameter("Active", SqlDbType.Bit);
                parms[5].Value = chkActive.Checked;

                parms[6] = new SqlParameter("LastUser",SqlDbType.UniqueIdentifier);
                parms[6].Value = MainParent.EmpWorking.Employee.Id;

                SqlHelper.ExecuteNonQuery(CommandType.Text, sb.ToString(), parms);

                _selectedRow.Cells["CardTypeID"].Value = cboCardType.Value;
                _selectedRow.Cells["VIP"].Value = chkVIP.Checked;
                _selectedRow.Cells["FromDate"].Value = parms[2].Value;
                _selectedRow.Cells["ToDate"].Value = parms[3].Value;
                _selectedRow.Cells["Active"].Value = chkActive.Checked;
                UpdateCard(_selectedRow);
                MessageBox.Show("Thông tin thẻ xe đã cập nhật.");
                this.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        #endregion

        #region Constructors
        public EditCard(UltraGridRow selectedRow)
        {
            InitializeComponent();
            _selectedRow = selectedRow;
        }
        #endregion
        
    }
}
